var qiniu = require("../lib/qiniu");
module.exports = {
	prefix: "/file",
	post: {
		"/upload/image/base64": function() {
			var self = this;
			var body = self.request.body;
			var base64_data = body.img_base64;
			if (!base64_data) {
				throw "参数错误"
			}
			var matches = base64_data.match(/^data:([A-Za-z-+\/]+);base64,(.+)$/);

			if (matches.length !== 3) {
				return new Error('Invalid input string');
			}
			var file_type = matches[1];
			if (file_type.indexOf("image\/") !== 0) {
				throw "上传的文件不是图片类型"
			}
			var file_buffer = new Buffer(matches[2], 'base64');

			qiniu.uploadBuff(file_buffer, function(err, ret) {
				if (err) {
					console.dir(err);
					throw "QINIU Error:(" + err.code + ")" + err.error
					return;
				}
				self.body = ResponObj("json", ret);
			});
		},
	}
}